var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: false,   //是否显示面板指示点（轮播图）
    autoplay: true,         //是否自动切换（轮播图）
    interval: 6000,         //自动切换时间间隔（轮播图）
    duration: 1000,         //滑动动画时长（轮播图）
    firstCategory: '',      //一级分类集合
    firstCategoryId: '',    //一级分类id标识
    jdBanners: '',          //京东广告位
    goodsList: [],          //商品集合
    hotGoodsList: '',       //爆款商品集合
    jpGoodsList: '',        //推荐商品集合
    orderType: '',          //排序 字段
    startFlag: 2,           //分页标识
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    var codeid = options.codeid

    if (!options){
      that.getGoodsList('', codeid, '', 0, 10)

      that.setData({
        firstCategoryId: codeid
      })
    }

    that.getFirstCategory();  //一级分类
    that.getbanners();        //获取京东轮播图
    that.getHotGoodsList();   //获取热卖商品
    that.getJpGoodsList();    //获取推荐商品
    that.getHeight();         //计算高度
  },

  /**
   * 获取 高度
   */
  getHeight: function () {
    var that = this;

    var query = wx.createSelectorQuery();
    var searchHeight = "";
    var goodstypeHeight = "";
    //获取头部高度
    query.select('#jdSeachInputCon').boundingClientRect()
    query.select('#categoryListAllCon').boundingClientRect()
    query.exec(function (res) {
      searchHeight = res[0].height;
      goodstypeHeight = res[1].height;

      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            ConHeight: res.windowHeight - searchHeight - goodstypeHeight
          });
        }
      });
    });
  },

  /**
   * 一级分类
   */
  getFirstCategory: function () {
    var that = this;
    wx.request({
      url: apiHost + "/mobile/yh/categorys", //仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          that.setData({
            firstCategory: res.data.data,
            firstCategoryId: res.data.data[0].codeid
          })
        } else {
          that.showMsg(msg);
        }
      }
    })
  },

  /**
   * 分类点击事件
   */
  categoryFri: function (e) {
    var that = this;
    var cateId = e.currentTarget.dataset.cateid;

    console.log(cateId)
    that.getGoodsList('', cateId, '', 0, 10)

    that.setData({
      firstCategoryId: cateId,
      orderType: ''
    })
  },

  /**
   * 获取京东轮播图
   */
  getbanners: function () {
    var that = this;
    wx.request({
      url: apiHost + "/mobile/yh/banners", //仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if (code == "10000") {
          that.setData({
            jdBanners: res.data.data
          })
        } else {
          that.showMsg(msg);
        }
      }
    })
  },

  /**
   * 获取爆款商品
   */
  getHotGoodsList : function(){
    var that = this;
    that.getGoodsList('hot','','',0,10)
  },

  getJpGoodsList: function () {
    var that = this;
    that.getGoodsList('jingpin', '', '', 0, 10)
  },

  /**
   * 获取 商品
   */
  getGoodsList: function ( goodsType , categoryId, orderType, start, length) {
    var that = this;

    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/goods/list", //仅为示例，并非真实的接口地址
      data: {
        'type':'yh',
        goodsType: goodsType,
        categoryCode: categoryId,
        orderType: orderType,
        start: start,
        length: length
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          var goodsData = res.data.data.list;

          if (goodsType!=''){
            that.getTypeGoodsList(goodsType,goodsData);
          }else{
            that.goodsPaging(start,goodsData);
          }
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 爆款和推荐商品
   */
  getTypeGoodsList : function(goodsType,goodsData){
    var that= this;
    
    if (goodsType=='hot'){
      that.setData({
        hotGoodsList: goodsData
      })
    }
    if (goodsType == 'jingpin'){
      that.setData({
        jpGoodsList: goodsData
      })
    }
  },

  /**
   * 京东商品下拉
   */
  scrollLower: function () {
    var that = this;
    var cateId = that.data.firstCategoryId;
    var start = that.data.startFlag;
    var orderType = that.data.orderType;

    that.getGoodsList('', cateId, '',start, 10);
  },

  /**
   * 商品分页
   */
  goodsPaging: function (start,goodsData){
    var that = this;

    if (start > 1) {
      var goodsLists = that.data.goodsList;
      // 追加数据
      for (var i = 0; i < goodsData.length; i++) {
        goodsLists.push(goodsData[i])
      }
      start = parseInt(start) + 1;
    } else {
      var goodsLists = goodsData;
      start = 2;
    }
    console.log(goodsLists)
    that.setData({
      goodsList: goodsLists,
      startFlag: start
    })
  },


  /**
   * 跳转商品详情
   */
  goYhGoodsDetail: function (e) {
    var that = this;
    var spuid = e.currentTarget.dataset.spuid;

    wx.navigateTo({
      url: "../../goods/yhjGoodsDetail/yhjGoodsDetail?spuid=" + spuid,
    })
  },

  /***
   * 搜索商品  跳搜索订单页
  */
  searchInput: function (e) {
    var value = e.detail.value;
    this.goPage('../../goods/searchgoods/searchgoods?value=' + value)
  },

  /**
 * 跳转页面
 */
  goPage: function (url) {
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    })
    wx.navigateTo({
      url: url,
      success: function () {
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})